layui.use(['form','layer','table','util','laydate'],function (){
    let form = layui.form,layer = layui.layer,table = layui.table,util = layui.util,laydate = layui.laydate;

    let dateElem = $('.form-dateElem');
    //渲染日期模块，最大日期为当前日期
    layui.each(dateElem,function () {
        laydate.render({
            elem:this,
            //最大的日期比今天多几天
            max:0,
        })
    })

    //监听table 工具栏，修改受理状态：档案数字化、数字化完成
    table.on('toolbar(fistDataTable)',function (obj) {

        //获取监听事件
        let layEvent = obj.event;
        //获取所有的选中的数据
        let companyData = layui.table.checkStatus("dataTableId").data;
        if (companyData.length <= 0){
            layer.msg("请选择一条数据",{icon:3,time:1500});
            return;
        }
        if (companyData.length > 1){
            layer.msg("一次只能选择一条数据",{icon:3,time:1500});
            return;
        }

        if (valueIsEqual('1',companyData[0].ciBrowCd)){
            layer.msg("该档案已经借出，无法再次借出",{icon:3,time:1500});
            return;
        }

        setValue("companyName",companyData[0].ciNm);
        setValue("companyCreditCode",companyData[0].ciCreditCode);
        setValue("companyRegistId",companyData[0].ciRegistId);
        setValue("companyCode",companyData[0].ciMgeCode);
        setValue("companyBussId",companyData[0].ciBussId);
        setValue("companyType",companyData[0].ciArchTpye);
        setValue("companyCheckDt",companyData[0].ciCheckDt);
        setValue("companyAresTolNum",companyData[0].ciAresTolNum);

        if (valueIsEqual(layEvent,'borrow')){
            top.layer.open({
                title:'档案借出',
                type:2,
                resize:false,
                area:['550px','650px'],
                content: '/archivesReadOut/readInfo',
                end:function (obj) {
                    selectCompanyInfoList();
                }
            });
        }
    });

})

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/9/22
* @description: 初始化生成table数据
*/
$(function () {
    selectCompanyInfoList();
})

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/9/22
* @description: 生成table数据
*/
function selectCompanyInfoList() {
    //起始日期
    let stDt = $('#stDt').val();
    //截止日期
    let lmDt = $('#lmDt').val();
    let stDtClean = valueClean(stDt,"-");
    let lmDtClean = valueClean(lmDt,"-");
    if (!valueIsEmpty(stDt) && ! valueIsEmpty(lmDt) && stDtClean > lmDtClean){
        top.layer.alert("起始日期不能大于截止日期");
        return;
    }

    //查询条件类型
    let srchReqFir = $("select[name = 'srchReqFir']").val();
    //查询条件
    let qryReqFir = $("input[name = 'qryReqFir']").val().trim();
    let jsonData = {
        "srchReqFir" : srchReqFir,
        "qryReqFir" : qryReqFir,
        "stDt" : stDt,
        "lmDt" : lmDt
    }
    //时间戳
    let tableTitle = '借阅企业信息表'+layui.util.toDateString(new Date(),'yyyyMMddHHmmss');
    layui.table.render({
        elem:'#dataTable',
        id:'dataTableId',
        url:'/business/acceptAdmin/queryCompanyInfo',
        parseData:function (result) {
            return{
                "code":result.code,
                "msg":result.msg,
                "count":result.data.pageCount,
                "data":result.data.list
            }
        },
        size:'sm',
        //分页的起止页号，每页条数
        page:{
            curr:1,
            limit:15,
            limits:[15,30,50,100]
        },
        //自定义的页码参数名称、每页条数参数名称
        request:{
            pageName:'pageNum',
            limitName:'pageSize'
        },
        //table的高度
        height:500,
        //查询条件，json格式
        where:jsonData,
        cols:[[
            {type:'checkbox'},
            {type:'numbers',title:'序号'},
            {field:'ciRegistId',title:'注册号'},
            {field:'ciCreditCode',title:'统一社会信用代码'},
            {field:'ciNm',title:'企业名称',width:300},
            {field:'ciPripalNm',title:'法人'},
            {field:'ciMgeNm',title:'管辖机关'},
            {field:'ciArchTpye',title:'档案类型',width: 100},
            {field:'ciAresTolNum',title:'页数',width: 100,templet:function (d) {
                    return d.ciAresTolNum + ' 页';
                }},
            {field:'ciCheckDt',title:'核准日期',width:100,templet:function (d) {
                    return layui.util.toDateString(d.ciRegistDt,"yyyy-MM-dd");
                }},
            {field:'ciBrowCd',title:'是否借出',width: 100,align:'center',templet:function (d) {
                    if (valueIsEqual(d.ciBrowCd,'1')){
                        return '<span style="color: red;">'+'已借出'+'</span>';
                    }else {
                        return '未借出';
                    }
                }},
            {field:'ciMgeCode',title:'管辖机关代码',hide:true},
            {field:'ciBussId',title:'主键',hide:true},
        ]],
        //头部工具条
        toolbar: '#totalToolBar',
        //导出时候excel文件标题
        title:tableTitle
    });
}

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/12/2
* @description: 获取选中行的业务申请编号集合
*/
function getCiCreditCodeList(companyData) {
    let ciCreditCodeList = "";
    for (let i=0;i < companyData.length; i++){
        if (valueIsEqual(ciCreditCodeList,"")){
            ciCreditCodeList = companyData[i].ciCreditCode ;
        }else {
            ciCreditCodeList = ciCreditCodeList + "," + companyData[i].ciCreditCode;
        }
    }
    return ciCreditCodeList;
}